package com.yanq.liancar.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxSPTool;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.MyOBDAdapter;
import com.yanq.liancar.adapter.OBDAdapter;
import com.yanq.liancar.bean.MyOBD;
import com.yanq.liancar.bean.OBDBean;
import com.yanq.liancar.bean.OBDModel;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.net.ApiService;
import com.yanq.liancar.net.util.ProgressSubscriber;
import com.yanq.liancar.util.DensityUtil;
import com.yanq.liancar.util.Utils;
import com.yanq.liancar.util.VerticalSpaceItemDecoration;
import com.yanq.liancar.view.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyOBDActivity extends BaseToolbarActivity {

    @BindView(R.id.rv_myOBD)
    RecyclerView mRecyclerView;

    @BindView(R.id.rl_noOBD)
    LinearLayout noCar;
    @BindView(R.id.ll_OBD)
    LinearLayout llCar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_right2)
    ImageView ivRight2;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.common_toolbar)
    Toolbar commonToolbar;
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.chooseChepai)
    TextView chooseChepai;
    @BindView(R.id.tv_bangdingOBD)
    TextView tvBangdingOBD;

    private List<MyOBD.DateBean> mMyOBDs = new ArrayList<>();
    private MyOBDAdapter mMyOBDAdapter;
    private boolean isC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_obd);
        Utils.setStatusBar(this, true, false);
        ButterKnife.bind(this);

        mTvTitle.setText("我的OBD");
        mIvRight.setVisibility(View.VISIBLE);
        mIvRight.setImageResource(R.mipmap.tianjia);
        mRecyclerView.addItemDecoration(new VerticalSpaceItemDecoration(getResources().getDimensionPixelSize(R.dimen.dp_1)));
        //initOkGo();
        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindOBD();
            }
        });
    }

    private void initOBD() {
        mHttpUtil.getOBD(new ProgressSubscriber<OBDBean>(mContext) {
            @Override
            public void next(OBDBean obdBean) {
                List<OBDBean.DataBean> data = obdBean.getData();
                if (data != null && data.size() > 0) {
                    noCar.setVisibility(View.GONE);
                    llCar.setVisibility(View.VISIBLE);
                    mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                    mRecyclerView.setAdapter(new OBDAdapter(mContext, data));
                } else {
                    noCar.setVisibility(View.VISIBLE);
                    llCar.setVisibility(View.GONE);
                }
            }
        }, UserBean.getcToken(mContext));
    }

    @Override
    protected void onResume() {
        super.onResume();
        //initOkGo();
        initOBD();
    }

    /*获取OBD*/
    private void initOkGo() {
        OkGo.<String>get("http://test19.value19.com" + "/Obd.php/Index/obdvehicle")
                .tag(this)
                .params("id", RxSPTool.getString(this, "UserId"))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        MyOBD bean = gson.fromJson(response.body(), MyOBD.class);
                        mMyOBDs = new ArrayList<>();
                        if (bean.getStatus().equals("true")) {
                            mMyOBDs = bean.getDate();
                            if (mMyOBDs.isEmpty() || mMyOBDs.size() == 0) {
                                noCar.setVisibility(View.VISIBLE);
                                llCar.setVisibility(View.GONE);
                            } else {
                                noCar.setVisibility(View.GONE);
                                llCar.setVisibility(View.VISIBLE);
                            }

                            final LinearLayoutManager layoutManager = new LinearLayoutManager(MyOBDActivity.this,
                                    LinearLayoutManager.VERTICAL, false);
                            mRecyclerView.setLayoutManager(layoutManager);

                            mMyOBDAdapter = new MyOBDAdapter();
                            mRecyclerView.setAdapter(mMyOBDAdapter);
                            mMyOBDAdapter.setNewData(mMyOBDs);

                            mMyOBDAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    switch (view.getId()) {
                                        case R.id.chakan:
                                            startActivity(new Intent(MyOBDActivity.this, CarDoctorActivity.class));
                                            break;
                                    }
                                }
                            });
                            mMyOBDAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent = new Intent(MyOBDActivity.this, BindOBDActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("OBDPos", position);
                                    intent.putExtras(bundle);
                                    startActivity(intent);

                                }
                            });
                        } else {
                            noCar.setVisibility(View.VISIBLE);
                            llCar.setVisibility(View.GONE);
                        }
                    }

                });
    }

    private void showSelectCarDialog() {
        final Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_selectcar, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(this, 32f);
        params.bottomMargin = DensityUtil.dp2px(this, 15f);
        contentView.setLayoutParams(params);


        TextView positiveButton = (TextView) contentView.findViewById(R.id.positiveButton);
        TextView negativeButton = (TextView) contentView.findViewById(R.id.negativeButton);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();

            }
        });
        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                return;
            }
        });
        bottomDialog.show();
    }

    @OnClick({R.id.chooseChepai, R.id.tv_bangdingOBD})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chooseChepai:
                showSelectCarDialog();
                break;
            case R.id.tv_bangdingOBD:
                bindOBD();
                break;
        }
    }

    private void bindOBD() {
        BindOBDActivity.start(mContext, -1, null, null);
    }
}